from interface import user_interface
from lib import common
from interface import bank_interface
from interface import shop_interface
from interface import admin_interface

base_logger = common.get_logger('base')

user_info = {
    'user': None,
}



def register_admin():
    print('注册管理员功能')
    while True:
        user = input('请输入管理员名：').strip()
        if user == 'bitten':
            pwd = input('请输入管理员密码:').strip()
            re_pwd = input('请确认管理员密码:').strip()
            if pwd == re_pwd:

                msg = user_interface.register_interface(user,pwd)
                be_admin = user_interface.be_admin_interface(user)
                if msg:
                    print(msg)
                    break
                else:
                    print('注册失败')
            else:
                print('密码不一致，请重新输入')
        else:
            print('你不是管理员')
            break



# 注册
def register():
    print('注册功能')
    while True:
        user = input('请输入用户名：').strip()
        flag = user_interface.check_user_interface(user)
        if flag:
            print('用户已存在，请重新输入')
            continue
        pwd = input('请输入密码：').strip()
        re_pwd = input('请确认输入密码').strip()
        if pwd == re_pwd:
            msg = user_interface.register_interface(user,pwd)
            if msg:
                print(msg)
                break
            else:
                print('注册失败')
        else:
            print('密码不一致，请重新输入')



# 登录
def login():

    while True:
        user = input('请输入用户名').strip()
        lock_state = user_interface.check_user_lock(user)
        flag = user_interface.check_user_interface(user)
        if not flag:
            print('用户不存在，请重新输入')
            continue
        if lock_state == False:
            pwd = input('请输入密码').strip()

            flag,msg = user_interface.login_interface(user,pwd)
            if flag:
                print(msg)
                user_info['user'] = user
                break
            else:
                print(msg)
        else:
            print('用户已被锁定，请联系管理员')
            continue


@common.login_auth
def check_bal():
    print('查看余额')
    bal = user_interface.check_bal_interface(user_info['user'])
    print(bal)

@common.login_auth
def withdraw():
    while True:
        money = input('请输入要提取的金额：').strip()
        if not money.isdigit():
            print('请输入数字')
            continue

        money = int(money)
        flag,msg = bank_interface.withdraw_interface(
            user_info['user'],money)
        if flag:
            print(msg)
            break
        else:
            print(msg)

@common.login_auth
def repay():
    while True:
        money = input('请输入要还款的金额：').strip()
        if not money.isdigit():
            print('请输入数字')
            continue
        money = int(money)
        flag,msg = bank_interface.repay_interface(user_info['user'],money)
        if flag:
            print(msg)
            break

@common.login_auth
def transfer():
    while True:
        to_user = input('请输入转账目标用户:').strip()
        flag = user_interface.check_user_interface(to_user)
        if not flag:
            print('目标用户不存在!')
            continue

        money = input('请输入转账金额:').strip()
        if not money.isdigit():
            print('请输入数字!')
            continue

        money = int(money)

        flag, msg = bank_interface.transfer_interface(
            to_user, user_info['user'], money)

        if flag:
            print(msg)
            break
        else:
            print(msg)

@common.login_auth
def check_flow():
    flow_list = bank_interface.check_flow_interface(user_info['user'])
    for flow in flow_list:
        print(flow)

@common.login_auth
def shopping():
    goods_list = [
        ['二锅头', 20],
        ['威士忌', 150],
        ['五粮液', 1800],
        ['西蓝花', 5000],
    ]

    # 获取用户余额
    user_balance = bank_interface.check_balance_interface(user_info['user'])

    # 购物车
    shopping_cart = {}


    # 初始总额
    cost = 0

    while True:
        # 打印商品信息
        for index,good_price in enumerate(goods_list):
            print(index,good_price)

        choice = input('请选择商品编号或者输入q退出：').strip()

        if choice.isdigit():
            choice = int(choice)

            if choice >= 0 and choice < len(goods_list):
                # 拿到商品的名称和价格
                good_name,good_price = goods_list[choice]

                if good_name in shopping_cart:
                    shopping_cart[good_name] += 1
                else:
                    shopping_cart[good_name] = 1
                cost += good_price

            else:
                print('请输入正确的编号')
        elif choice == 'q':
            commit = input('是否确认结账，请输入y/n:').strip()
            if commit == 'y':

                flag, msg = shop_interface.buy_shop_interface(
                    user_info['user'],cost)

                if flag:
                    print(msg)
                    break
                else:
                    print(msg)

            elif commit == 'n':
                shop_interface.add_shop_cart_interface(user_info['user'],shopping_cart)
            break


        else:
            print('请输入数字')

@common.login_auth
def check_shop_cart():
    shop_cart = shop_interface.check_shop_interface(user_info['user'])
    if not shop_cart:
        print('购物车是空的,请选择购物车功能.')

    print(shop_cart)


@common.login_auth
def logout():
    flag, msg = user_interface.logout_interface()
    if flag:
        print(msg)


@common.login_auth
def lock_user():
    print('冻结用户')
    user = input('请输入要被冻结的用户：').strip()

    flag = user_interface.check_user_interface(user)

    if flag:
        msg = admin_interface.lock_interface(user)
        print(msg)
    else:
        print('用户不存在')

@common.login_auth
def unlock_user():
    print('解冻用户')
    user = input('请输入要被解冻的用户：').strip()
    flag = user_interface.check_user_interface(user)

    if flag:
        msg =admin_interface.unlock_interface(user)
        print(msg)

    else:
        print('用户不存在')


@common.login_auth
def change_limit():
    print('修改用户额度')
    user = input('请输入要修改额度的用户：').strip()
    flag = user_interface.check_user_interface(user)
    if not flag:
        print('用户不存在')
        return

    limit = input('请输入修改额度').strip()

    if limit.isdigit():
        limit = int(limit)

        msg = admin_interface.change_limit_interface(user,limit)
        print(msg)

    else:
        print('请输入数字')





admin_func_dic = {
    '1': lock_user,
    '2': unlock_user,
    '3': change_limit,
}



@common.login_auth
def admin_sys():
    admin_state = user_interface.check_admin_lock(user_info['user'])

    while admin_state == True:
        print('''
        1.冻结用户
        2.解冻用户
        3.用户额度
        ''')

        choice = input('请选择管理员功能:q退出').strip()

        if choice in admin_func_dic:
            admin_func_dic[choice]()
        elif choice == 'q':
            print('退出成功')
            break
        else:
            print('请重新输入')
            continue
    else:
        print('你不是管理员')



func_dic ={
    '0': register_admin,
    '1': register,
    '2': login,
    '3': check_bal,
    '4': withdraw,
    '5': repay,
    '6': transfer,
    '7': check_flow,
    '8': shopping,
    '9': check_shop_cart,
    '10': logout,
    '11': admin_sys,
}

def run():
    while True:
        print('''
        0.注册管理员
        1.注册
        2.登录
        3.查看余额
        4.提现
        5.还款
        6.转账
        7.查看流水
        8.购物车功能
        9.查看购物车
        10.注销
        11.管理员功能
        ''')

        choice = input('请选择功能编号：').strip()

        if choice == 'q':
            break

        if choice in func_dic:
            func_dic[choice]()
        else:
            print('请输入正确编号')

